<script lang="ts" setup name="XtxCarousel">
import {BannerItem } from '@/types/data'
import { onUnmounted, PropType, ref } from 'vue';
// v3 的父传子接收的数据可以配合protype 指定类型
//  const props = defineProps({
//   list:{
//     type:Array as PropType<BannerItem[]>,
//     required:true
//   },
//   // 间隔时间
//   interval:{
//     type:Number,
//     default:2000
//   },
//   // 是否开启自动轮播
//   autoplay:{
//     type:Boolean,
//     default:false
//   },
//   // 是否展示小圆点
//   isShowdots:{
//     type:Boolean,
//     default:false
//   }
// })
// ts定义类型
 const { 
  list,
  interval=2000,
  autoplay=false,
  isShowdots=false
  } = defineProps<{
  list:BannerItem[],
  interval?:number,
  autoplay?:boolean,
  isShowdots:boolean
}>()
const ActiveIndex = ref(0) //默认激活第0项
// 上一张
const prev = ()=>{
ActiveIndex.value--
if(ActiveIndex.value<0){
//  最后一张：  重新赋值为数组的长度 
  ActiveIndex.value=list.length-1
}
}
// 下一张
const next =()=>{
ActiveIndex.value++
if(ActiveIndex.value>list.length-1){
  ActiveIndex.value=0
}
}
// 自动轮播: 鼠标移入移出 定时器 组件销毁清除定时器
let timeerId = -1
// 鼠标移出开启
const start = ()=>{
  if(!autoplay) return //定制自动播放
 timeerId = window.setInterval(()=>{
    next()
  },interval)
}
// 自动轮播
start()
// 鼠标移入关闭
const  stop = ()=>{
 clearInterval(timeerId)
}
onUnmounted(()=>[
  stop()
])
</script>

<template>
  <div class="xtx-carousel" @mouseenter="stop"  @mouseleave="start" >
    <!-- 轮播图的图片 -->
    <ul class="carousel-body">
      <li v-for="(item,index) in list" :key="item.id" class="carousel-item" 
      :class="{fade:index===ActiveIndex}" >
        <RouterLink to="/">
          <img
            :src="item.imgUrl"
            alt=""
          />
        </RouterLink>
      </li>
    </ul>
    <!-- 左右箭头 -->
    <a href="javascript:;" class="carousel-btn prev" @click="prev"
      ><i class="iconfont icon-angle-left"></i
    ></a>
    <a href="javascript:;" class="carousel-btn next" @click="next"
      ><i class="iconfont icon-angle-right"></i
    ></a>
    <!-- 数量小圆点 -->
    <div v-if="isShowdots" class="carousel-indicator">
      <span :class="{active:index===ActiveIndex}" @mouseenter="ActiveIndex=index" v-for="(item,index) in list" :key="item.id"></span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>